/*
 * TranslateJPanel.java
 * 
 *  Copyright (C) - 2008 - Juanbc - Aleval
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.bts.translateTool.view;

import com.bts.translateTool.back.*;
import com.bts.util.DMesg;
import com.bts.util.AboutJDialog;
import com.bts.translateTool.Info;
import java.awt.Window;

/**
 * Panel de traductor
 * @author  juan
 */
public class TranslateJPanel extends javax.swing.JPanel {

    /** Constante que representa que la aplicacion por defecto se detendra al cerrar */
    public static final int EXIT_ON_CLOSE = javax.swing.WindowConstants.EXIT_ON_CLOSE;
    /** Constante que representa que la aplicacion por defecto se ocultara al cerrar */
    public static final int DISPOSE_ON_CLOSE = javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
    private GoogleTranslateConnection gears;
    /** Determina cual es la operacion de la aplicacion por defecto*/
    private int defaulCloseOperation;
    /** ventana que contiene la aplicación*/
    private Window parent;

    /**
     * Constructor, recibe una palabra a traducirm, un codigo que representa la accion que se llevara 
     * a cabo al cerar la aplicacion y la ventana que contiene la aplicación
     * @param str
     * @param defaulCloseOperation
     * @param parent
     */
    public TranslateJPanel(String str, int defaulCloseOperation, Window parent) {
        gears = new GoogleTranslateConnection();
        this.defaulCloseOperation = defaulCloseOperation;
        initComponents();
        this.jTextArea2.setText(str);
        this.chargeComboTo();
        this.parent = parent;
    }

    /**
     * retorna el texto traducido
     * @return String
     */
    public String getTranslation() {
        return this.jTextArea1.getText();
    }

    /**
     * retorna el codigo de la accion que se llevara a cabo al cerrarse la aplicacion
     * @return codigo_de_cierre
     */
    public int getDefaulCloseOperation() {
        return defaulCloseOperation;
    }

    /**
     * cierra la aplicacion verificando su accion por defecto
     */
    private void closeOperation() {
        switch (this.defaulCloseOperation) {
            case EXIT_ON_CLOSE:
                System.exit(0);
                break;
            case DISPOSE_ON_CLOSE:
                parent.dispose();
            default: /*faltaria tirar una excepcion*/ break;
        }
    }

    /**
     * carga el combo de idiomas a los cuales se traduce eliminando al que esta selecionado
     * en el combo de idiomas desde los cuales se traduce
     */
    private void chargeComboTo() {
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(gears.getLanguagesNames()));
        this.jComboBox2.removeItemAt(this.jComboBox1.getSelectedIndex());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Opciones"));
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(gears.getLanguagesNames()));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel2.add(jComboBox1);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText(">>>");
        jPanel2.add(jLabel3);

        jPanel2.add(jComboBox2);

        jButton1.setText("Traducir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.setMaximumSize(null);
        jPanel1.setLayout(new java.awt.GridLayout(2, 0));

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Original"));

        jTextArea2.setColumns(20);
        jTextArea2.setFont(new java.awt.Font("Dialog", 0, 14));
        jTextArea2.setLineWrap(true);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        jPanel1.add(jScrollPane2);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Traducci\u00f3n"));

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(new java.awt.Font("Dialog", 0, 14));
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jPanel1.add(jScrollPane1);

        jLabel1.setText("Powered by Google!");

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jButton3.setText("Cerrar");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setInheritsPopupMenu(true);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        jButton2.setText("Acerca de...");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 633, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 342, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addGap(24, 24, 24))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        this.chargeComboTo();
    }//GEN-LAST:event_jComboBox1ActionPerformed
/**
 * traduce el texto
 * @param evt
 */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String from = String.valueOf(this.jComboBox1.getSelectedItem());
        String to = String.valueOf(this.jComboBox2.getSelectedItem());
        String str = this.jTextArea2.getText();
        try {
            str = gears.getTranslation(str, from, to);
        } catch (GoogleTranslateApiException e) {
            DMesg.err(e.getLocalizedMessage());
            javax.swing.JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Google ERROR", javax.swing.JOptionPane.ERROR_MESSAGE);
            str = "";
        } finally {
            this.jTextArea1.setText(str);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.closeOperation();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String info=Info.PROGRAM_NAME +" "+ Info.VERSION  + "\nCopyright (C) "+ Info.YEAR +"  "+ Info.AUTHORS + "\n" +
            "This is free software, and you are welcome to redistribute it.";
        String name=Info.PROGRAM_NAME;
        String authors= "Main Programers:\n  "  + Info.AUTHORS + "\n\nGraphic Design:\n  " + Info.GRAPHIC_DESIGN;
        String lic=Info.LICENSE;
        javax.swing.ImageIcon icon = new javax.swing.ImageIcon(getClass().getResource("/com/bts/translateTool/resources/splash.png"));
        AboutJDialog.open(name, info, authors, lic, icon);
        
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}
